<template>
  <el-dialog
    title="产品列表"
    :visible.sync="dialogVisible"
    :modal="true"
    :appendToBody="true"
    width="1100px"
    v-dialog-drag
    :close-on-click-modal="false"
    top="5vh"
    @close="handleClose()"
    @open="handleOpen()"
  >
    <div class="selected-content">
      <div v-for="item in selectionList" :key="item.erpItemCode" class="item">
        <span>{{ item.erpItemDescCn }}</span>
        <span class="del-item-btn" @click="handleDelSelection(item)"
          ><i class="el-icon-close"></i
        ></span>
      </div>
    </div>
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
      :disabled="disabledSearch"
    >
      <el-form-item style="margin-right: 0">
        <organizationCascader
          :arrData="queryParams"
          :showData="{
            factory: true,
            workshop: false,
            line: false,
            workSection: false,
          }"
          :clearData="{
            factory: true,
          }"
          :isAllList="true"
          @change="queryParamsChange"
        ></organizationCascader>
      </el-form-item>

      <el-form-item prop="searchValue">
        <el-input
          v-model="queryParams.searchValue"
          placeholder="请输入关键字"
          clearable
          size="small"
          @keyup.enter.native="getList"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="getList"
          >查询</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <TableSelect
      ref="table"
      :data="list"
      :selectionList.sync="selectionList"
      :selectableCallBack="selectableCallBack"
      rowKey="erpItemCode"
      v-loading="tableLoading"
    >
      <el-table-column label="物料编码" prop="erpItemCode"> </el-table-column>
      <el-table-column label="物料名称（中文）" prop="erpItemDescCn">
      </el-table-column>
      <el-table-column label="物料名称（英文）" prop="erpItemDescEn">
      </el-table-column>
      <el-table-column label="单位" prop="erpItemUnit"> </el-table-column>
    </TableSelect>
    <pagination
      :total="pager.total"
      :page.sync="pager.pageNum"
      :limit.sync="pager.pageSize"
      @pagination="getList"
    />
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="beSureSelect()">保 存</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { listErpItem } from "@/api/prodInfor/material";
import TableSelect from "@/components/TableSelect";
import organizationCascader from "@/views/components/organizationCascader";
export default {
  components: { TableSelect, organizationCascader },
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    // 不可选回调，返回true或false，传参为row
    selectableCallBack: {
      type: Function,
    },
    // 是否多选
    multiple: {
      type: Boolean,
      default: false,
    },
    // 默认筛选条件
    defaultSearch: {
      type: Object,
      default: null,
    },
    // 默认展示[{erpItemCode:"",erpItemDescCn:""}]
    defaultValues: {
      type: Array,
      default: () => {},
    },
    // 是否禁用筛选条件
    disabledSearch: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      queryParams: {
        searchValue: "",
        factoryDeptCode: "",
        erpItemCategoryName: "产成品",
      },
      list: [],
      tableLoading: false,
      pager: {
        total: 0,
        pageNum: 1,
        pageSize: 10,
      },
      selectionList: [],
    };
  },
  computed: {
    dialogVisible: {
      set(val) {
        this.$emit("update:visible", val);
      },
      get() {
        return this.visible;
      },
    },
  },
  methods: {
    queryParamsChange({ factoryDeptCode }) {
      this.queryParams.factoryDeptCode = factoryDeptCode;
    },
    handleClose() {
      this.queryParams = {
        searchValue: "",
        factoryDeptCode: "",
      };
      this.list = [];
      this.selectionList = [];
      this.tableLoading = false;
      this.pager.total = 0;
      this.pager.pageNum = 1;
    },
    async handleOpen() {
      this.selectionList = this.defaultValues?.length
        ? [...this.defaultValues]
        : [];

      // 默认筛选条件
      if (this.defaultSearch) {
        this.queryParams = Object.assign(this.queryParams, this.defaultSearch);
      }

      // 查询列表
      this.getList();
    },
    // 获取用户列表
    getList() {
      this.tableLoading = true;
      const { pageNum, pageSize } = this.pager;

      const params = {
        query: {
          ...this.queryParams,
        },
        pageNum,
        pageSize,
      };
      listErpItem(params)
        .then((res) => {
          this.list = res.rows || [];
          this.pager.total = res.total;
        })
        .finally(() => {
          this.tableLoading = false;
        });
    },
    //重置
    resetQuery() {
      this.queryParams = {
        searchValue: "",
        factoryDeptCode: "",
      };
      this.pager.pageNum = 1;
      this.getList();
    },
    handleDelSelection(item) {
      if (!item) {
        return;
      }
      if (this.$refs.table) {
        this.$refs.table.handleDelSelection(item);
      }
    },
    beSureSelect() {
      const selection = this.selectionList;
      if (!this.multiple) {
        if (selection.length > 1) {
          this.$message({
            message: "最多选择一条数据!",
            type: "warning",
          });
          return;
        }
      }
      this.$emit("closeDialog", selection || []);
      this.dialogVisible = false;
    },
  },
};
</script>
<style lang="scss" scoped>
.selected-content {
  width: 100%;
  height: 88px;
  padding: 10px;
  border: 1px solid #dfe6ec;
  margin-bottom: 20px;
  overflow-y: auto;
  .item {
    height: 28px;
    line-height: 28px;
    margin-right: 10px;
    margin-bottom: 10px;
    padding: 0 10px;
    font-size: 14px;
    display: inline-block;
    vertical-align: middle;
    background: rgb(234, 248, 254);
    color: #409eff;
    .del-item-btn {
      margin-left: 10px;
      cursor: pointer;
    }
  }
}
::v-deep {
  .el-dialog .el-dialog__body {
    padding-top: 10px;
  }
  .el-form-item {
    margin-bottom: 15px;
  }
}
</style>
